/*
 * Converts form to json String
 * Check if deals with dropdown/checkboxes
 * 
 * <SerailizedArray> formData - Serialized Array of form
 * 
 * return <String> jsonString - String obtained from converting Array to jsonString
 */
function converter(formData) {
	var jsonString = "{";
	var flag = 0;
	for ( var i = 0; i < formData.length; i++) {
		var name = formData[i].name;
		var value = formData[i].value;
		if (flag == 0) {
			jsonString = jsonString + '"' + name + '":"' + value + '"';
			flag = 1;
		} else
			jsonString = jsonString + ',"' + name + '":"' + value + '"';
	}
	jsonString = jsonString + '}';
	return (jsonString);
}

/*
 * Converts table row to json object
 * Only deals with inputs
 * Needs to be modified to take checkbox, dropdown values
 * 
 * <tr> tr - Table row to be converted to json object
 * 
 * return <json> - Json object obtained by converting table row
 */
function tableRowToObj(tr) {

	var i = 0, j = 0, keys = [], obj, ret = [];
	obj = {};

	tr.find('td').each(
			function() {
				if ($(this).children('input').length > 0
						|| $(this).children('button').length > 0) {
				} else {
					keys.push($(this).attr('class'));
					obj[keys[j++]] = $(this).text();
				}
			});

	tr.find('td.hidden').each(
			function() {
				if ($(this).children('input').length > 0
						|| $(this).children('button').length > 0) {
				} else {
					keys.push($(this).attr('class'));
					obj[keys[j++]] = $(this).text();
				}
			});

	ret.push(JSON.stringify(obj));
	return ret;
}

/*
 * Fills values of table columns of particular row to Modal form
 * 
 * <tr> tr - Table row to be converted\
 * <String> preIndex - Char('u') { ModalFormInputName(usectionId) - TableColName(sectionId) }
 * <String> formName - Modal Form Name
 * 
 * return <void>
 */
function tableRowToModalForm(tr, preIndex, formName) {

	var curRowId = tr.attr('id');

	tr.find('td').each(
			function() {
				if ($(this).children('input').length > 0
						|| $(this).children('button').length > 0) {
				} else {
					$(
							'form#' + formName + ' input[id=' + preIndex
									+ $(this).attr('class') + ']').val(
							$('#' + curRowId + ' td.' + $(this).attr('class'))
									.text());
				}
			});

	tr.find('td.hidden').each(
			function() {
				if ($(this).children('input').length > 0
						|| $(this).children('button').length > 0) {
				} else {
					$(
							'form#' + formName + ' input[id=' + preIndex
									+ $(this).attr('class') + ']').val(
							$('#' + curRowId + ' td.' + $(this).attr('class'))
									.text());
				}
			});
}

/*
 * Convert json with values to columns of table row
 * Called after starting table row
 * Handles hidden columns
 * 
 * <String> toAppend - String modified and returned
 * <String> item - json input
 * <String[]> hiddenCols - Array of hidden column names
 * 
 * return <String> toAppend - Output String after <json> converted to <String>
 */
function jsonItemToString(toAppend, item, hiddenCols) {

	$.each(item, function(key, value) {
		var found = $.inArray(key, hiddenCols) > -1;
		if (found == false) {
			toAppend = toAppend + "<td class='" + key + "'>" + value + "</td>";
		} else {
			toAppend = toAppend + "<td class='" + key + "' hidden='true'>"
					+ value + "</td>";
		}

		// alert(key + "," + value);
	});

	return toAppend;
}

/*
 * Adds button with data-toggle='modal' data-target = "modalName"
 * Called in cases where data is present in Modal
 * 
 * <String> toAppend - String modified and returned
 * <String> buttonValue - Display Name of String
 * <String> buttonClass - css class on button
 * <String> buttonId - Attribute Id of button required for calling
 * <String> modalName - Attribute name of bootstrap modal
 * 
 * return <String> toAppend - Modified String
 */
function addButtonWithModalToTable(toAppend, buttonValue, buttonClass,
		buttonId, modalName) {
	toAppend = toAppend + "<td><input type='button' value='" + buttonValue
			+ "' class='" + buttonClass + "' id='" + buttonId
			+ "' data-toggle='modal' data-target='#" + modalName + "' /></td>";
	return toAppend;
}

/*
 * Adds button without data-toggle='modal' data-target = "modalName"
 * Called in cases where no data is present in Modal
 * 
 * <String> toAppend - String modified and returned
 * <String> buttonValue - Display Name of String
 * <String> buttonClass - css class on button
 * <String> buttonId - Attribute Id of button required for calling
 * 
 * return <String> toAppend - Modified String
 */
function addButtonWithoutModalToTable(toAppend, buttonValue, buttonClass,
		buttonId) {
	toAppend = toAppend + "<td><input type='button' value='" + buttonValue
			+ "' class='" + buttonClass + "' id='" + buttonId + "' /></td>";
	return toAppend;
}

/*
 * Adds checkbox to table row
 * 
 * <String> toAppend - String modified and returned
 * <String> checkboxName - Attribute name of checkbox
 * <String> checkboxId - Attribute ID of checkbox
 * 
 * return <String> toAppend - Modified String
 */
function addCheckboxToTableRow(toAppend, checkboxName, checkboxId) {
	toAppend = toAppend + "<td><input type='checkbox' name='" + checkboxName
			+ "' title='Select Row ' id=" + checkboxId + " /></td>";
	return toAppend;
}

/*
 * Starts table row
 * 
 * <String> toAppend - String modified and returned
 * <String> rowId - RowId
 * 
 * return <String> toAppend - Modified String
 */
function startTableRow(toAppend, rowId) {
	toAppend = toAppend + "<tr id = " + rowId + ">";
	return toAppend;
}

/*
 * End table row
 * 
 * <String> toAppend - String modified and returned
 * 
 * return <String> toAppend - Modified String
 */
function endTableRow(toAppend) {
	toAppend = toAppend + "</tr>";
	return toAppend;
}

/*
 * Creates form group divs on Modal form
 * <String[id,name,placeholder,hidden,readonly]> elements
 * 
 * elements[i][0] - id
 * elements[i][1] - name
 * elements[i][2] - placeholder
 * elements[i][3] - hidden
 * elements[i][4] - readonly
 * 
 * return <String> toAppend - Modified String
 */
function createFormGroupDivs(elements) {
	var toAppend = "";
	
	for ( var i = 0; i < elements.length; i++) {
		toAppendDiv = "";
		
		if(!elements[i][3])
		{
			toAppendDiv = toAppendDiv + "<div class='form-group'>";// hidden='" + elements[i][3] + "'>";
		} else {
			toAppendDiv = toAppendDiv + "<div class='form-group' hidden='" + elements[i][3] + "'>";
		}
		
		toAppendDiv = toAppendDiv + "<label class='control-label' for='" + elements[i][0] + "'>" + elements[i][2] + "</label>";
		
		if(!elements[i][4])
		{
			toAppendDiv = toAppendDiv + "<input class='form-control input-xs' id='" + elements[i][0] + "' name='" + elements[i][1] + "' placeholder='" + elements[i][2] + "' type='text' />";
		} else {
			toAppendDiv = toAppendDiv + "<input class='form-control input-xs' id='" + elements[i][0] + "' name='" + elements[i][1] + "' placeholder='" + elements[i][2] + "' type='text' readonly />";
		}
		toAppendDiv = toAppendDiv + "</div>";
		toAppend = toAppend + toAppendDiv;
	}
	return toAppend;
}

/*
 * Adds Pagination footer row to Table
 * 
 * <String> toAppend - String modified and returned
 * 
 * return <String> toAppend - Modified String
 */
function attachPaginationBarToTable(toAppend) {
	toAppend += 	"<tfoot>";
	toAppend += 		"<tr>";
	toAppend += 			"<th colspan='7' class='ts-pager form-horizontal'>";
	toAppend += 				"<button type='button' class='btn first'><i class='icon-step-backward glyphicon glyphicon-step-backward'></i></button>";
	toAppend += 				"<button type='button' class='btn prev'><i class='icon-arrow-left glyphicon glyphicon-backward'></i></button>";
	toAppend += 				"<span class='pagedisplay'></span>";
	toAppend += 				"<button type='button' class='btn next'><i class='icon-arrow-right glyphicon glyphicon-forward'></i></button>";
	toAppend += 				"<button type='button' class='btn last'><i class='icon-step-forward glyphicon glyphicon-step-forward'></i></button>";
	toAppend += 				"<select class='pagesize input-mini' title='Select page size'>";
	toAppend += 					"<option selected='selected' value='10'>10</option>";
	toAppend += 					"<option value='20'>20</option>";
	toAppend += 					"<option value='30'>30</option>";
	toAppend += 					"<option value='40'>40</option>";
	toAppend += 				"</select>";
	toAppend += 				"<select class='pagenum input-mini' title='Select page number'></select>";
	toAppend += 			"</th>";
	toAppend += 		"</tr>";
	toAppend += 	"</tfoot>";
	return toAppend;
}

/*
 * Creates table header
 * 
 * <String> toAppend - String Modified and returned
 * <String[]> theaders - Array of table headers
 * <boolean> createSelectAllCheckboxFlag - <true> to create select all checkbox in header row 
 * 
 * return <String> toAppend - Modified String
 */
function createTableHeader(toAppend, theaders, createSelectAllCheckboxFlag) {
	
	toAppend += "<thead>";
	toAppend += 	"<tr>";
	
	if(createSelectAllCheckboxFlag) {		
		toAppend +=		"<th><input id='chkall' name='chkall' type='checkbox' /></th>";
	}
	
	for ( var i = 0; i < theaders.length; i++) {
		if(theaders[i][1]) {
			toAppend += 		"<th hidden='true'>" + theaders[i][0] + "</th>";
		} else {
			toAppend += 		"<th>" + theaders[i][0] + "</th>";
		}
	}
	
	toAppend += 	"</tr>";
	toAppend += "</thead>";

	return toAppend;
}

/*
 * Clears values from all <input> any form. 
 * 
 * <String> FormID
 * 
 * return <void>
 */
function clearModalFormValues(formName) {
	
	$("#" + formName + " :input").each(function(){
	   $(this).val('');
	});	
	return ;
}
